//
//  HomeJoinViewController.swift
//  jiahui
//
//  Created by 3tee on 2024/11/29.
//

import UIKit

class HomeJoinViewController: UIViewController {
    var loginModel: LoginModel?
    @IBOutlet weak var collectionHeight: NSLayoutConstraint!
    let cellId = "HomeJoinCollectionViewCell"
    let itemPading: CGFloat = 8.0
    var width:CGFloat = 0.0
    var itemSize :CGSize = CGSizeZero
    var models:[[String:String]] = []
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configData()
        setupUI()
        // Do any additional setup after loading the view.
    }

    
    override func viewDidAppear(_ animated: Bool) {
        let data = UserDefaultsPlugin.obj(forKey: Constant.key_loginModel) as! Data
        if let loginModel2 = try? JSONDecoder().decode(LoginModel.self, from: data){
            print("已经登陆过 保存的model = \(loginModel2)")
            self.loginModel = loginModel2
        }
        
    }
    
    func setupUI() {
        width = (UIScreen.main.bounds.size.width - itemPading * 4) / 3
        itemSize = CGSize(width: width, height: width )
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = itemPading
        collectionView.collectionViewLayout = layout

        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: cellId, bundle: .main), forCellWithReuseIdentifier: cellId)
        collectionHeight.constant = width + itemPading
    }
    
    func configData(){
        let dic1 = ["title":"召开会议","img":"ic_hosting"]
        let dic2 = ["title":"开始会议","img":"ic_joinning"]
        let dic3 = ["title":"我的会议","img":"ic_myMeeting"]
        models.append(dic1)
        models.append(dic2)
        models.append(dic3)
        print(models)
    }
}


extension HomeJoinViewController: UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dic = models[indexPath.row]
        if let title = dic["title"], title == "召开会议" {
            print("点击了召开会议")
        }
        if let title = dic["title"], title == "开始会议" {
            print("点击了开始会议")
            guard loginModel != nil else {
                print("获取房间数据失败")
                return
            }
            let joiningVC = JoiningViewController()
            joiningVC.loginModel = self.loginModel
            self.navigationController?.pushViewController(joiningVC, animated: true)

        }
        if let title = dic["title"], title == "我的会议" {
            print("点击了我的会议")
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    
    }
    
    
}

extension HomeJoinViewController: UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HomeJoinCollectionViewCell
        cell.updateCell(dic: models[indexPath.row])
        return cell
    }
    
}


extension HomeJoinViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return itemSize
    }
}
